//
//  SYWelcomeViewController.swift
//  SingWeibo
//
//  Created by shiying on 15/12/17.
//  Copyright © 2015年 shiying. All rights reserved.
//

import UIKit
import SDWebImage

class SYWelcomeViewController: UIViewController {

    //获得约束
    private var iconViewConstraint: NSLayoutConstraint?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        prepareUI()
    
        // 先获取上一次保存头像
        setIconView()
        //加载用户头像
        SYUseAccount.loadUserAccount()?.loadUserInfo({ (error) -> () in
            // 判断是有错误
            if error != nil {
                print("用户信息出错了")
                return
            }
            // 获取用户数据没有出错，加载最新的头像
            self.setIconView()
        })

    }
    //MARK: - 显示当前用户头像
    func setIconView() {
        if let avatar_large = SYUseAccount.loadUserAccount()?.avatar_large {
            let url = NSURL(string: avatar_large)!
            print("头像url:\(url)")
            // 显示当前用户的头像
            self.iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))
        }
    }

    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        iconViewConstraint?.constant = -(UIScreen.mainScreen().bounds.height - 160)
        UIView.animateWithDuration(0.8, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
                self.view.layoutIfNeeded()
            }) { (_) -> Void in
                self.welcomeLabel.alpha = 0
                self.welcomeLabel.hidden = false
                UIView.animateWithDuration(3, animations: { () -> Void in
                    self.welcomeLabel.alpha = 1.0
                    }, completion: { (_) -> Void in
                        print("动画完成")
                        (UIApplication.sharedApplication().delegate as! AppDelegate).switchRootViewController(true)
                })
        }

    }
    
    func prepareUI() {
    
        view.addSubview(backGroundView)
        view.addSubview(iconView)
        view.addSubview(welcomeLabel)
        
        // 设置背景
        backGroundView.ff_Fill(view)
        
        //设置头像
        let cons =  iconView.ff_AlignInner(type: ff_AlignType.BottomCenter, referView: view, size: CGSize(width: 85, height: 85), offset: CGPoint(x: 0, y: -160))
        iconViewConstraint = iconView.ff_Constraint(cons, attribute: NSLayoutAttribute.Bottom)
        
        // 设置欢迎文字
        welcomeLabel.ff_AlignVertical(type: ff_AlignType.BottomCenter, referView: iconView, size: nil, offset: CGPoint(x: 0, y: 16))
        
/*
        backGroundView.translatesAutoresizingMaskIntoConstraints = false
        iconView.translatesAutoresizingMaskIntoConstraints = false
        welcomeLabel.translatesAutoresizingMaskIntoConstraints = false
        
       view.addConstraint(NSLayoutConstraint(item: backGroundView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: backGroundView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: backGroundView, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: backGroundView, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        iconViewConstraint =  NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160)
        view.addConstraint(iconViewConstraint!)
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 85))
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 85))
        
        
        view.addConstraint(NSLayoutConstraint(item: welcomeLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: welcomeLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
*/
    }

    
    //MARK: - 懒加载
    private lazy var iconView: UIImageView = {
        let iconView = UIImageView()
        
        iconView.image = UIImage(named: "avatar_default_big")
        iconView.layer.cornerRadius = 42.5
        iconView.layer.masksToBounds = true
    
        return iconView
    }()
    
    private lazy var backGroundView = UIImageView(image: UIImage(named: "ad_background"))
    
    private lazy var welcomeLabel:UILabel = {
        let welcomeLabel = UILabel()
        
        welcomeLabel.text = "欢迎回来"
        welcomeLabel.font = UIFont.systemFontOfSize(14)
        welcomeLabel.hidden = true
        
        return welcomeLabel
    }()

}
